import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'

// 解决路由重复报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

// 路由懒加载
const AdminLogin = () => import(/* webpackChunkName: "admin" */ '../components/admin/Login')
const AdminHome = () => import(/* webpackChunkName: "admin" */ '../components/admin/Home')
const AdminWelcome = () => import(/* webpackChunkName: "admin" */ '../components/admin/Welcome')
const AdminUsers = () => import(/* webpackChunkName: "admin" */ '../components/admin/user/Users')
const AdminMerchants = () => import(/* webpackChunkName: "admin" */ '../components/admin/merchant/Merchants')
const AdminMerchantDetails = () => import(/* webpackChunkName: "admin" */ '../components/admin/merchant/MerchantDetails')
const AdminRemark = () => import(/* webpackChunkName: "admin" */ '../components/admin/remark/Remark')
const AdminReport = () => import(/* webpackChunkName: "admin" */ '../components/admin/remark/Report')
const AdminShowReport1 = () => import(/* webpackChunkName: "admin" */ '../components/admin/remark/ShowReport1')
const AdminShowReport2 = () => import(/* webpackChunkName: "admin" */ '../components/admin/remark/ShowReport2')
const AdminShowReport3 = () => import(/* webpackChunkName: "admin" */ '../components/admin/remark/ShowReport3')

const Home = () => import(/* webpackChunkName: "home" */ '../components/Home')
const Welcome = () => import(/* webpackChunkName: "home" */ '../components/Welcome')
const Login = () => import(/* webpackChunkName: "home" */ '../components/Login')
const Register = () => import(/* webpackChunkName: "home" */ '../components/Register')

const UserInfo = () => import(/* webpackChunkName: "user" */ '../components/user/UserInfo')
const UserRemark = () => import(/* webpackChunkName: "user" */ '../components/user/UserRemark')
const UserFavorites = () => import(/* webpackChunkName: "user" */ '../components/user/UserFavorites')
const UserPicture = () => import(/* webpackChunkName: "user" */ '../components/user/UserPicture')
const UserHome = () => import(/* webpackChunkName: "user" */ '../components/user/UserHome')
const UserUpdatePassword = () => import(/* webpackChunkName: "user" */ '../components/user/UserUpdatePassword')
const RestaurantDetails = () => import(/* webpackChunkName: "user" */ '../components/user/RestaurantDetails')
const FoodList = () => import(/* webpackChunkName: "user" */ '../components/user/FoodList')
const FoodDetails = () => import(/* webpackChunkName: "user" */ '../components/user/Food')
const Other = () => import(/* webpackChunkName: "user" */ '../components/user/Other')

const MerchantHome = () => import(/* webpackChunkName: "merchant" */ '../components/merchant/MerchantHome')
const MerchantInfo = () => import(/* webpackChunkName: "merchant" */ '../components/merchant/MerchantInfo')
const Food = () => import(/* webpackChunkName: "merchant" */ '../components/merchant/Food')
const Image = () => import(/* webpackChunkName: "merchant" */ '../components/merchant/Image')
const MerchantReport = () => import(/* webpackChunkName: "merchant" */ '../components/merchant/Report')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', name: 'Welcome', component: Welcome },
      { path: '/login', name: 'Login', component: Login },
      { path: '/register', name: 'Register', component: Register },
      {
        path: '/merchant/home',
        name: 'MerchantHome',
        component: MerchantHome,
        redirect: '/merchant/info',
        children: [
          { path: '/merchant/info', name: 'MerchantInfo', component: MerchantInfo },
          { path: '/merchant/food', name: 'Food', component: Food },
          { path: '/merchant/image', name: 'Image', component: Image },
          { path: '/merchant/report', name: 'MerchantReport', component: MerchantReport }
        ]
      },
      {
        path: '/user/home',
        name: 'UserHome',
        component: UserHome,
        children: [
          { path: '/user/info', name: 'UserInfo', component: UserInfo },
          { path: '/user/remark', name: 'UserRemark', component: UserRemark },
          { path: '/user/favor', name: 'UserFavorites', component: UserFavorites },
          { path: '/user/password', name: 'UserUpdatePassword', component: UserUpdatePassword },
          { path: '/user/picture', name: 'UserPicture', component: UserPicture }
        ]
      },
      {
        path: '/user/merchant',
        name: 'RestaurantDetails',
        component: RestaurantDetails,
        children: [
          { path: '/user/food_list', name: 'FoodList', component: FoodList },
          { path: '/user/food', name: 'FoodDetails', component: FoodDetails },
          { path: '/user/other', name: 'Other', component: Other }
        ]
      }
    ]
  },
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: AdminLogin
  },
  {
    path: '/admin/home',
    name: 'AdminHome',
    component: AdminHome,
    redirect: '/admin/welcome',
    children: [
      { path: '/admin/welcome', name: 'AdminWelcome', component: AdminWelcome },
      { path: '/admin/users', name: 'AdminUsers', component: AdminUsers },
      { path: '/admin/merchants', name: 'AdminMerchants', component: AdminMerchants },
      { path: '/admin/merchant/details', name: 'AdminMerchantDetails', component: AdminMerchantDetails },
      { path: '/admin/remark', name: 'AdminRemark', component: AdminRemark },
      { path: '/admin/report', name: 'AdminReport', component: AdminReport },
      { path: '/admin/show_report1', name: 'AdminShowReport1', component: AdminShowReport1 },
      { path: '/admin/show_report2', name: 'AdminShowReport2', component: AdminShowReport2 },
      { path: '/admin/show_report3', name: 'AdminShowReport3', component: AdminShowReport3 }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

// 路由导航守卫
// to 将要访问的路径
// from 从哪个路径转过来
// next 是一个函数，next()表示放行，next('/xxx')表示强制跳转到某个页面
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register' || to.path === '/admin/login' ||
      to.path === '/home' || to.path === '/welcome') {
    return next()
  }
  // 获取token
  const token = window.sessionStorage.getItem('token')
  if (!token) {
    Message.info('请先登录')
    return next('/login')
  }
  next()
})

export default router
